package com.ouc.dataMange.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ouc.dataMange.domain.SysDataSetStatistics;
import com.ouc.dataMange.mapper.SysDatamangeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ouc.dataMange.mapper.SysUploadFileMapper;
import com.ouc.dataMange.domain.SysUploadFile;
import com.ouc.dataMange.service.ISysUploadFileService;

/**
 * uploadFileService业务层处理
 *
 * @author ouc
 * @date 2025-07-16
 */
@Service
public class SysUploadFileServiceImpl implements ISysUploadFileService {
  @Autowired
  private SysUploadFileMapper sysUploadFileMapper;

  @Autowired
  private SysDatamangeMapper sysDatamangeMapper;

  /**
   * 查询uploadFile
   *
   * @param fileId uploadFile主键
   * @return uploadFile
   */
  @Override
  public SysUploadFile selectSysUploadFileByFileId(Integer fileId) {
    return sysUploadFileMapper.selectSysUploadFileByFileId(fileId);
  }

  /**
   * 查询uploadFile列表
   *
   * @param sysUploadFile uploadFile
   * @return uploadFile
   */
  @Override
  public List<SysUploadFile> selectSysUploadFileList(SysUploadFile sysUploadFile) {
    return sysUploadFileMapper.selectSysUploadFileList(sysUploadFile);
  }

  /**
   * 根据数据集，查询数据统计列表
   *
   * @param sysUploadFile
   * @return
   */
  @Override
  public List<SysDataSetStatistics> selectDataSet(SysUploadFile sysUploadFile) {
    List<SysDataSetStatistics> sysDataSetStatisticResult = new ArrayList<>();
    List<SysUploadFile> sysUploadFileList = sysUploadFileMapper.selectDataSet(sysUploadFile);
    for (int i = 0; i < sysUploadFileList.size(); i++) {
      if (sysUploadFileList.get(i).getDatamangeId2() != null) {
        SysUploadFile sysUploadFileSelect = new SysUploadFile();
        sysUploadFileSelect.setDatamangeId2(sysUploadFileList.get(i).getDatamangeId2());
        List<SysUploadFile> list = sysUploadFileMapper.selectSysUploadFileList(sysUploadFileSelect);
        SysDataSetStatistics dataSetStatistic = new SysDataSetStatistics();
        dataSetStatistic.setDataSetId(sysUploadFileList.get(i).getDataSetId());
        dataSetStatistic.setDatamangeId(sysUploadFileList.get(i).getDatamangeId2());
        dataSetStatistic.setDatamangeName(sysDatamangeMapper.selectSysDatamangeById(sysUploadFileList.get(i).getDatamangeId2()).getDataSet());
        dataSetStatistic.setDataNum(list.size());
        Long sumSize = 0L;
        for (int j = 0; j < list.size(); j++) {
          sumSize+= list.get(j).getFileInfo();
        }
        dataSetStatistic.setDataSize(sumSize);
        sysDataSetStatisticResult.add(dataSetStatistic);
      }
    }
    return sysDataSetStatisticResult;
  }

  /**
   * 查询最近查看列表（5个）
   * @param sysUploadFile
   * @return
   */
  @Override
  public List<SysUploadFile> selectSysUploadFileListByViewTime(SysUploadFile sysUploadFile){
    return sysUploadFileMapper.selectSysUploadFileListByViewTime(sysUploadFile);
  }

  /**
   * 查询最新上传列表（5个）
   * @param sysUploadFile
   * @return
   */
  @Override
  public List<SysUploadFile> selectSysUploadFileListByUpload(SysUploadFile sysUploadFile){
    return sysUploadFileMapper.selectSysUploadFileListByUpload(sysUploadFile);
  }

  /**
   * 新增uploadFile
   *
   * @param sysUploadFile uploadFile
   * @return 结果
   */
  @Override
  public int insertSysUploadFile(SysUploadFile sysUploadFile) {
    return sysUploadFileMapper.insertSysUploadFile(sysUploadFile);
  }

  /**
   * 修改uploadFile
   *
   * @param sysUploadFile uploadFile
   * @return 结果
   */
  @Override
  public int updateSysUploadFile(SysUploadFile sysUploadFile) {
    return sysUploadFileMapper.updateSysUploadFile(sysUploadFile);
  }

  /**
   * 批量删除uploadFile
   *
   * @param fileIds 需要删除的uploadFile主键
   * @return 结果
   */
  @Override
  public int deleteSysUploadFileByFileIds(Integer[] fileIds) {
    return sysUploadFileMapper.deleteSysUploadFileByFileIds(fileIds);
  }

  /**
   * 删除uploadFile信息
   *
   * @param fileId uploadFile主键
   * @return 结果
   */
  @Override
  public int deleteSysUploadFileByFileId(Integer fileId) {
    return sysUploadFileMapper.deleteSysUploadFileByFileId(fileId);
  }
}
